using OnePF;
using System;
using System.Collections.Generic;
using UnityEngine;

public class IAPManager : MonoBehaviour
{
	public event Action<string> onPurchaseSuccess;

	private void Start()
	{
		OpenIABEventManager.billingSupportedEvent += billingSupportedEvent;
		OpenIABEventManager.billingNotSupportedEvent += billingNotSupportedEvent;
		OpenIABEventManager.queryInventorySucceededEvent += queryInventorySucceededEvent;
		OpenIABEventManager.queryInventoryFailedEvent += queryInventoryFailedEvent;
		OpenIABEventManager.purchaseSucceededEvent += purchaseSucceededEvent;
		OpenIABEventManager.purchaseFailedEvent += purchaseFailedEvent;
		OpenIABEventManager.consumePurchaseSucceededEvent += consumePurchaseSucceededEvent;
		OpenIABEventManager.consumePurchaseFailedEvent += consumePurchaseFailedEvent;
		OpenIABEventManager.restoreSucceededEvent += RestoreSuccess;
		OpenIABEventManager.restoreFailedEvent += RestoreFailed;
		InitBilling();
	}

	public void InitBilling()
	{
		string value = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm94qrptFeJUvzZO4qhJ0l8MgjnHfdSBqhXIn3KAFPnBEnhxBRLMJCW1Xn26LKJVSAwB5+hGIhVL8uaH1Fe9AW/LviDr1eCblZH2CgLmL6eFOgqyXH1L0gmkCvcXgF4WAVS84kPzBEetKpyo/r4WZfQTGTtSzGpTwZJNjiX6s3Eojrbr4aOf8A3FO6+I98GEHMIMZA4mjV/1giFpGJCxPJ5nL5lQzPToUjt2B8ouRNWmOHrbW3v7U/Ir9WUhqqDNODwZU2QQVxkr+BA5DDCmMNF/B9YTzVyrqwYQTKr04c5UyA48MffKKdan9Q5ISqESucTUB7aCvOa0vI8JbwgGcewIDAQAB";
		Options options = new Options();
		options.checkInventoryTimeoutMs = 20000;
		options.discoveryTimeoutMs = 10000;
		options.checkInventory = false;
		options.verifyMode = OptionsVerifyMode.VERIFY_SKIP;

		options.storeSearchStrategy = SearchStrategy.INSTALLER_THEN_BEST_FIT;
	}

	public void Purchase(string namesku)
	{

	}

	private void PurchaseSuccess(string sku)
	{
		if (this.onPurchaseSuccess != null)
		{
			this.onPurchaseSuccess(sku);
		}
	}

	private void billingSupportedEvent()
	{
		UnityEngine.Debug.Log("billingSupportedEvent");
	}

	private void billingNotSupportedEvent(string error)
	{
		UnityEngine.Debug.Log("billingNotSupportedEvent: " + error);
	}

	private void queryInventorySucceededEvent(Inventory inventory)
	{
		UnityEngine.Debug.Log("query success");
		List<Purchase> allPurchases = inventory.GetAllPurchases();
		foreach (Purchase item in allPurchases)
		{
			if (item.PurchaseState == 0)
			{
				if (item.Sku.Equals("stickfight_weapon_xm"))
				{
					PersistenData.addNewPurchaseData("Weapon3");
				}
				else if (item.Sku.Equals("stickfight_weapon_ak"))
				{
					PersistenData.addNewPurchaseData("Weapon5");
				}
				else if (item.Sku.Equals("stickfight_weapon_m99"))
				{
					PersistenData.addNewPurchaseData("Weapon8");
				}
				else if (item.Sku.Equals("stickfight_weapon_laser"))
				{
					PersistenData.addNewPurchaseData("Weapon9");
				}
			}
		}
	}

	private void queryInventoryFailedEvent(string error)
	{
		UnityEngine.Debug.Log("queryInventoryFailedEvent: " + error);
	}

	private void purchaseSucceededEvent(Purchase purchase)
	{
		UnityEngine.Debug.Log("purchaseSucceededEvent: " + purchase);
		PurchaseSuccess(purchase.Sku);
	}

	private void purchaseFailedEvent(int errorCode, string errorMessage)
	{
		UnityEngine.Debug.Log("purchaseFailedEvent: " + errorMessage);
	}

	private void consumePurchaseSucceededEvent(Purchase purchase)
	{
		UnityEngine.Debug.Log("consumePurchaseSucceededEvent: " + purchase);
	}

	private void consumePurchaseFailedEvent(string error)
	{
		UnityEngine.Debug.Log("consumePurchaseFailedEvent: " + error);
	}

	public void RestorePurchasedItem()
	{
	}

	private void RestoreSuccess()
	{
		UnityEngine.Debug.Log("restore success");
	}

	private void RestoreFailed(string message)
	{
		UnityEngine.Debug.Log("error" + message);
	}
}
